package GraphTheory;

import java.util.Scanner;
import java.util.Stack;

class Node1136
{	public int value;
	public Node1136 left,right;
	public Node1136(int v)
	{	value=v;
		left=null;right=null;
	}
	public boolean isLeaf()
	{	return (left==null && right==null);
	}
}
class Solver1136
{	int n;
	Stack<Integer> Rec=new Stack<Integer>();
	Node1136 root;
	
	public void Input()
	{	Scanner in=new Scanner(System.in);
		n=in.nextInt();
		for (int i=1;i<=n;i++) Rec.push(in.nextInt());
	}
	private void BuildTree(Node1136 root,Stack<Integer> stk)
	{	while (!stk.isEmpty())
		{	int f=stk.pop();
			Node1136 nd=root;
			while (true)
				if (f>nd.value)
				{	if (nd.right!=null) nd=nd.right;
					else
					{	nd.right=new Node1136(f);break;
					}
				}
				else
				{	if (nd.left!=null) nd=nd.left;
					else
					{	nd.left=new Node1136(f);break;
					}
				}
		}
	}	
	public void Solve()
	{	Input();
		root=new Node1136(Rec.pop());
		BuildTree(root,Rec);
		Output(root);
		System.out.println();
	}
	public void Output(Node1136 root)
	{	if (!root.isLeaf())
		{	if (root.right!=null) Output(root.right);
			if (root.left!=null) Output(root.left);
		}
		System.out.print(root.value+" ");
	}
}
public class P1136
{	public static void main(String[] args)
	{	Solver1136 solver=new Solver1136();
		solver.Solve();
	}
}
